package cn.appsys.controller.back;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.Page;
import cn.appsys.service.back.appinfo.BackAppinfoService;
import cn.appsys.service.front.appcategory.FrontAppcategoryService;
import cn.appsys.service.front.appversion.FrontAppversionService;
import cn.appsys.service.front.datadictionary.FrontDictionaryService;

/**
 * 后台控制器
 * 
 * @author administrader
 * 
 */
@Controller
@RequestMapping("/back")
public class BackAppController {

	@Resource
	private BackAppinfoService backAppinfoService;

	@Resource
	private FrontDictionaryService frontDictionaryService; // 字典数据逻辑接口

	@Resource
	private FrontAppcategoryService frontAppcategoryService; // appcategory逻辑接口

	@Resource
	private FrontAppversionService frontAppversionService; // 版本逻辑接口

	/**
	 * 负责按条件并分页查询待审核状态的App信息列表
	 * 
	 * @param querySoftwareName
	 *            查询参数一：软件名称
	 * @param queryFlatformId
	 *            查询参数二：所属平台的ID
	 * @param queryCategoryLevel1
	 *            查询参数三：一级类别的类别ID
	 * @param queryCategoryLevel2
	 *            查询参数四：二级类别的类别ID
	 * @param queryCategoryLevel3
	 *            查询参数五：三级类别的类别ID
	 * @param currPageNo
	 *            查询参数六：当前页码
	 * @return 查询到分页记录
	 */
	@RequestMapping("/list")
	public String list(
			@RequestParam(value = "querySoftwareName", required = false) String querySoftwareName,
			@RequestParam(value = "queryFlatformId", required = false) Integer queryFlatformId,
			@RequestParam(value = "queryCategoryLevel1", required = false, defaultValue = "0") Integer queryCategoryLevel1,
			@RequestParam(value = "queryCategoryLevel2", required = false, defaultValue = "0") Integer queryCategoryLevel2,
			@RequestParam(value = "queryCategoryLevel3", required = false, defaultValue = "0") Integer queryCategoryLevel3,
			@RequestParam(value = "currPageNo", required = false, defaultValue = "1") Integer currPageNo,
			Model model) {

		// 调用service层获取总的记录数
		Integer totalCount = backAppinfoService.queryTotalCount(
				querySoftwareName, queryFlatformId, queryCategoryLevel1,
				queryCategoryLevel2, queryCategoryLevel3);

		// 封装Page实体类
		Page page = new Page();
		page.setCurrPageNo(currPageNo); // 当前页面
		page.setPageSize(5);
		page.setTotalCount(totalCount); // 总的记录数

		Integer from = (page.getCurrPageNo() - 1) * page.getPageSize();

		// 调用service的按条件查询待审核状态的App信息列表信息并分页显示的方法
		List<AppInfo> appInfoList = backAppinfoService.queryAppInfo(
				querySoftwareName, queryFlatformId, queryCategoryLevel1,
				queryCategoryLevel2, queryCategoryLevel3, from,
				page.getPageSize());

		// 查询出状态信息
		List<DataDictionary> statusList = frontDictionaryService
				.findDataDictionaries("APP_STATUS");
		// 查询出所属平台信息
		List<DataDictionary> flatFormList = frontDictionaryService
				.findDataDictionaries("APP_FLATFORM");
		// 一级分类
		List<AppCategory> categoryLevel1List = frontAppcategoryService
				.queryById(null);

		List<AppCategory> categoryLevel2List = null; // 二级分类列表
		List<AppCategory> categoryLevel3List = null; // 三级分类列表
		if (queryCategoryLevel2 != null && queryCategoryLevel2 != 0) {
			// 查询二级分类信息
			categoryLevel2List = frontAppcategoryService
					.queryById(queryCategoryLevel2);
			model.addAttribute("categoryLevel2List", categoryLevel2List);
		}
		if (queryCategoryLevel3 != null && queryCategoryLevel3 != 0) {
			// 查询三级分类信息
			categoryLevel3List = frontAppcategoryService
					.queryById(queryCategoryLevel3);
			model.addAttribute("categoryLevel3List", categoryLevel3List);
		}

		// 将查询的数据进行反显
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryFlatformId", queryFlatformId);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);

		// 保存到model作用域中 在jsp页面中显示
		model.addAttribute("statusList", statusList); // 查询出状态信息
		model.addAttribute("flatFormList", flatFormList);// 查询出所属平台信息
		model.addAttribute("categoryLevel1List", categoryLevel1List);// 一级分类
		model.addAttribute("categoryLevel2List", categoryLevel2List);// 二级分类
		model.addAttribute("categoryLevel3List", categoryLevel3List);// 三级分类
		model.addAttribute("appInfoList", appInfoList); // 查询出appinfo数据
		model.addAttribute("pages", page); // 分页

		return "backend/applist";

	}

	/**
	 * 异步实现三级联动
	 * 
	 * @param pid
	 * @return
	 */
	@RequestMapping("/queryCategoryListByParentId")
	@ResponseBody
	public Object queryCategoryListByParentId(@RequestParam("pid") Integer pid) {
		List<AppCategory> appCategryList = frontAppcategoryService.queryById(pid);
		return appCategryList;
	}
	
	
	
	/**
	 * 审核详细信息
	 * 
	 * @param aid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/appcheckList")
	public String appcheckList(@RequestParam("aid") Integer aid,
			@RequestParam("vid") Integer vid, Model model) {
		// 审核详细信息-APP基础信息
		AppInfo appInfo = backAppinfoService.queryAppcheck(aid);
		// 审核信息详细-最新版本信息
		AppVersion appVersion = backAppinfoService.queryAppletViewer(vid);
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("appVersion", appVersion);
		return "backend/appcheck";
	}

	/**
	 * 审核是否通过
	 * @param appInfo
	 * @return
	 */
	@RequestMapping(value = "/checksave")
	public String appcheckUpdate(AppInfo appInfo) {

		if (appInfo.getStatus() == 2) {

			Integer aid = appInfo.getId();

			Integer status = 2;

			int count = backAppinfoService.queryAppUpdate(aid, status);

			if (count > 0) {
				return "redirect:/back/list"; // 修改成功跳转到后台系统管理列表页面
			} else {
				return "redirect:/back/appcheckList"; // 修改失败跳转到审核页面
			}

		} else if (appInfo.getStatus() == 3) {
			Integer aid = appInfo.getId();
			Integer status = 3;

			Integer count = backAppinfoService.queryAppUpdate(aid, status);

			if (count > 0) {
				return "redirect:/back/list"; // 修改成功跳转到后台系统管理列表页面
			} else {
				return "redirect:/back/appcheckList"; // 修改失败跳转到审核页面
			}

		} else {
			return "redirect:/back/appcheckList"; // 修改失败跳转到审核页面
		}
	}
}
